﻿@model OJS.Web.Areas.Contests.ViewModels.Results.ContestResultsViewModel

@using OJS.Web.Areas.Contests.Controllers
@using OJS.Web.ViewModels.Shared
@using Resource = Resources.Areas.Contests.Views.ResultsSimple

@{
    ViewBag.Title = string.Format(Resource.Title, Model.Name);
    ViewBag.Subtitle = string.Format(Resource.Subtitle, Model.TotalResults);
    string isOfficial = ViewBag.IsOfficial ? CompeteController.CompeteUrl : CompeteController.PracticeUrl;
}

@section Styles {
    <link href="/Content/Contests/results-page.css" rel="stylesheet" />
}

<ol class="breadcrumb">
    <li><a href="/">@Resource.Home</a></li>
    <li><a href="@ContestsHelper.GetUrl(Model.Id, Model.Name)">@Model.Name</a></li>
    <li class="active">@Resource.Simple_results</li>
</ol>

<h1>@ViewBag.Title</h1>
<h2>
    @ViewBag.Subtitle

    @if (User.IsAdmin())
    {
        <span>@Html.ActionLink(Resource.Detailed_results, "Full", new { id = Model.Id, official = ViewBag.IsOfficial }, new { @class = "btn btn-primary" })</span>
    }
</h2>

@if (Model.AllPages > 1)
{
    @Html.Partial("_Pagination", new PaginationViewModel { AllPages = Model.AllPages, CurrentPage = Model.CurrentPage, Url = "/Contests/" + isOfficial + "/Results/Simple/" + Model.Id + "?page=" })
}

<table class="table table-responsive table-bordered table-striped text-white text-center width100percent">
    <thead>
        <tr>
            <th>№</th>
            <th class="text-center">@Resource.User</th>
            <th class="text-center">@Resource.UserFullName</th>
            @{
                int problemCounter = 0;
                bool displayLink = Model.ContestCanBeCompeted || Model.ContestCanBePracticed;
                var contestLink = Model.ContestCanBeCompeted ? "Compete" : "Practice";
            }
            @foreach (var problem in Model.Problems)
            {
                if (problem.ShowResults || (Model.ContestCanBePracticed && !ViewBag.IsOfficial) || User.IsAdmin())
                {
                <th class="text-center">
                    @if (displayLink)
                    {
                        <a href="/Contests/@contestLink/Index/@Model.Id#@problemCounter">@problem.Name</a>
                        problemCounter++;
                    }
                    else
                    {
                        @problem.Name
                    }
                </th>
                }
            }
            <th class="text-center">@Resource.Total</th>
        </tr>
    </thead>
    <tbody>
        @{ var results = Model.Results.ToList(); }
        @for (int i = 0; i < results.Count; i++)
        {
            var participant = results[i];
            string className = User.Identity.Name == participant.ParticipantUsername ? "success" : string.Empty;
            <tr class="@className">
                <td>@((Model.CurrentPage - 1) * ResultsController.ResultsPageSize + i + 1)</td>
                <td><a href="/Users/Profile?id=@participant.ParticipantUsername">@participant.ParticipantUsername</a></td>
                <td>@participant.ParticipantFullName</td>
                @foreach (var participantResult in participant.ProblemResults)
                {
                    if (User.IsAdmin())
                    {
                        if (participantResult.BestSubmission == null)
                        {
                    <td>0</td>
                        }
                        else
                        {
                    <td><a href="/Contests/Submissions/View/@participantResult.BestSubmission.Id">@participantResult.BestSubmission.Points</a></td>
                        }
                    }
                    else if (participantResult.ShowResult)
                    {
                        if (participantResult.BestSubmission == null)
                        {
                    <td>0</td>
                        }
                        else
                        {
                    <td>@participantResult.BestSubmission.Points</td>
                        }
                    }
                }
                @if (User.IsAdmin())
                {
                    <td>@participant.AdminTotal</td>
                }
                else
                {
                    <td>@participant.Total</td>
                }
            </tr>
        }
    </tbody>
</table>

@if (Model.AllPages > 1)
{
    @Html.Partial("_Pagination", new PaginationViewModel { AllPages = Model.AllPages, CurrentPage = Model.CurrentPage, Url = "/Contests/" + isOfficial + "/Results/Simple/" + Model.Id + "?page=" })
}